﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using ScrumTable.BL.DM.DataManagement;
using ScrumTable.Config.ConfigManagement;
using ScrumTable.Common.Unity;
using ScrumTable.UI.ViewModel.Data;

#endregion

namespace ScrumTable.UI.ViewModel.Extension
{

    public static class ViewModelItemObservableCollectionExtensions
    {

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        private static ConfigManager _configManager;


        /// <summary>
        /// Gets the Elementmanager
        /// </summary>
        public static ConfigManager ConfigManager
        {
            get
            {
                if (_configManager == null)
                {
                    _configManager = ((IAppScreen)Application.Current.MainWindow).Container.Resolve<ConfigManager>();
                }
                return _configManager;
            }
        }
        #endregion


        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------


        /// <summary>
        /// Moves the items in the current list.
        /// </summary>
        /// <param name="newItems">A list of the new list.</param>
        /// <param name="containerItems">Reference to the current list. This list gets updated and returned</param>
        /// <returns></returns>
        public static ObservableCollection<IScrumItemViewModel<DomainBase>> CombineList(IEnumerable<IScrumItemViewModel<DomainBase>> newItems, ref ObservableCollection<IScrumItemViewModel<DomainBase>> containerItems)
        {

            var userStories = new ObservableCollection<IScrumItemViewModel<DomainBase>>(newItems);
            if (containerItems == null)
            {
                containerItems = userStories;
            }
            else
            {
                containerItems.UnionToLeft(userStories);
            }
            containerItems.SortDomainItems();
            return containerItems;
        }

        /// <summary>
        /// Sort the list
        /// </summary>
        /// <param name="collection"></param>
        public static void SortDomainItems(this ObservableCollection<IScrumItemViewModel<DomainBase>> collection)
        {
            collection.Sort(x => x.Ranking, ConfigManager.Config.IsStackRankDescending, item => item.BackEndId);
        }

         /// <summary>
        /// Sort the list
        /// </summary>
        /// <param name="collection"></param>
        public static void SortDomainItems(this ObservableDomainCollection<UserStoryViewModel, UserStory> collection)
        {
            collection.Sort(x => x.Ranking, ConfigManager.Config.IsStackRankDescending, item => item.BackEndId);
        }


        /// <summary>
        /// Sort the list
        /// </summary>
        /// <param name="collection"></param>
        public static void SortDomainItems(this ObservableCollection<UserStoryViewModel> collection)
        {
            collection.Sort(x => x.Ranking, ConfigManager.Config.IsStackRankDescending, item => item.BackEndId);
        }
        
        #endregion
    }
}
